<?PHP
require_once WACT_ROOT.'controller/controller.inc.php';
require_once APP_ROOT.'view/pub/layoutright.view.php';
require_once APP_ROOT.'model/common/user.php';
require_once APP_ROOT.'model/common/article.php';
require_once APP_ROOT.'model/common/sportmail.php';
require_once WACT_ROOT . 'controller/form.inc.php';
require_once WACT_ROOT . 'validation/rules/pattern.inc.php';
require_once APP_ROOT.'model/common/date.php';
require_once APP_ROOT . "model/common/site.php";

class ArticleEmailMessageView extends LayoutRightView {
	function ArticlesEmailMessageView($TemplateFile){
		parent::LayoutRightView($TemplateFile);

	}

	function prepare(&$controller, &$request, &$responseModel) {
		parent::prepare(&$controller, &$request, &$responseModel);
			if(!Session::isLogin())
			$this->Template->set('login',' ');

            
           $this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		   $this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		   $this->Template->set('SITE_TITLE',Site::getTitle());
		   $this->Template->set('title',Site::getTitle().' - Story was sent!');      

 
	}

}

class ArticlesEmailView extends LayoutFormRightView {
	function ArticlesEmailView($TemplateFile){
		parent::LayoutFormRightView($TemplateFile);

	}

	function prepare(&$controller, &$request, &$responseModel){
		parent::prepare(&$controller, &$request, &$responseModel);
		if(APP_NAME!=""){
			$this->Template->set('app_name',APP_NAME);
		} else $this->Template->set('app_name','.');


        $this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
	   

 
		if(!Session::isLogin())
			$this->Template->set('login',' ');

		$article_id=$request->getParameter('id');
		$type=$request->getParameter('type');

		$idel=$this->Form->getChild('article_id');
		$idel->setValue($article_id);
		$typeel=$this->Form->getChild('type');
		$typeel->setValue($type);

  
    
		$article=&Article::getRecord($article_id);
		if($type==1){
			$this->Template->set('title',Site::getTitle().' - Email story');
   		} else {
			$this->Template->set('title',Site::getTitle().' - Email story teaser');
			$this->Template->set('teaser',' ');
		}

		$datestr=$article->get('date_added');
		$mydate=strtotime($datestr);
        $tz =& new MyTimeZone();
        $mydate = $tz->timeGMT($mydate);
		$datestr = gmstrftime('%b %d, %Y',$mydate);
		$this->Template->set('date_added',$datestr);

		$prem=$article->get('premium');
		if($prem&&$prem==0) $prem=null;
		$this->Template->set('prem',$prem);

		$this->Template->set('headline',$article->get('headline'));

		$recepients=$this->Form->getChild('recepients');
		if(!$responseModel->get('recepients'))
			$recepients->setValue("List all the email recepients above, separated by commas with no spaces.");
		if($responseModel->get('message')){
			error_log("[1]");
			$this->Template->set('message',$responseModel->get('message'));
		}

	}

}

class ArticlesEmailPage extends PageController {
	function ArticlesEmailPage(){
		parent::PageController();
		$Form = new FormController();
       $Form->addChild('send', new ButtonController(new Delegate($this, 'send')));
		$Form->addRule(new RequiredRule('name'));
		$Form->addRule(new RequiredRule('email'));
		$Form->addRule(new PatternRule('recepients','/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}(\,[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,})*$/i'));

		$Form->addRule(new PatternRule('email','/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i'));

      	$this->addChild("email_form",$Form);
		$this->setDefaultChild('email_form');

        $mytemplate1 = 'pub/articles/message.html';
        $mytemplate2 = 'pub/articles/email.html';

		$this->addView('success',new ArticleEmailMessageView($mytemplate1));
		$this->setDefaultView(new ArticlesEmailView($mytemplate2));


	}

	function send(&$controller, &$request, &$responseModel){
		if($responseModel->isValid()) {
			$res=$responseModel->get('recepients');
			$check=$responseModel->get('tomyself');

			if(!$res&&!$check) {
				error_log("[2]$res:$check");
				$responseModel->set('message','You must enter at least one email in recepients');
				return;
			}

			$responseModel->set('message','');
			Sportmail::sendArticle($responseModel);
			return 'success';

		}

	}


}

?>
